package com.ld.wms.business.log.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.log.mapper.CameraMonitorInfoMapper;
import com.ld.wms.business.log.model.CameraMonitorInfoModel;
import com.ld.wms.business.log.service.CameraMonitorInfoService;
import com.ld.wms.business.log.vo.req.CameraMonitorInfoReqVo;
import com.ld.wms.business.log.vo.resp.CameraMonitorInfoRespVo;
import com.ld.wms.common.vo.GeneratePageListVo;
import com.ld.wms.exception.BusinessException;
import com.ld.wms.properties.CameraMonitorProperties;
import com.ld.wms.properties.LDProperties;
import com.ld.wms.utils.DateUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-06-05
 */
@Service
public class CameraMonitorInfoServiceImpl extends ServiceImpl<CameraMonitorInfoMapper, CameraMonitorInfoModel> implements CameraMonitorInfoService {

    @Autowired
    private CameraMonitorInfoMapper cameraMonitorInfoMapper;

    @Autowired
    private LDProperties ldProperties;

    @Override
    public void add(CameraMonitorInfoReqVo reqVo) {
        CameraMonitorInfoModel cameraMonitorInfoModel = new CameraMonitorInfoModel();
        BeanUtils.copyProperties(reqVo, cameraMonitorInfoModel);
        Date date = new Date();
        String dateStr = DateUtils.dateToStr(date, DateUtils.YYYY_MM_DD_HH_MM_SS2);
        cameraMonitorInfoModel.setTitle(dateStr);
        cameraMonitorInfoModel.setCreateTime(date);
        cameraMonitorInfoMapper.insert(cameraMonitorInfoModel);
    }

    @Override
    public GeneratePageListVo<CameraMonitorInfoRespVo> getPageList(Integer current, Integer size) {
        IPage<CameraMonitorInfoRespVo> iPage = new Page<>(current, size);
        IPage<CameraMonitorInfoRespVo> iPage1 = cameraMonitorInfoMapper.selectPageList(iPage);
        return new GeneratePageListVo<>(iPage1);
    }

    @Override
    public void getVideo(Long id, HttpServletResponse response) {
        try {
            CameraMonitorInfoModel cameraMonitorInfoModel = cameraMonitorInfoMapper.selectById(id);
            if (cameraMonitorInfoModel == null) {
                throw new BusinessException("未找到视频文件");
            }
            File file = new File(cameraMonitorInfoModel.getFilePath());
            //读取指定路径下面的视频
            InputStream in = new FileInputStream(file);
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            //创建存放视频内容的数组
            byte[] buff =new byte[1024];
            //所读取的内容使用n来接收
            int n;
            //当没有读取完时,继续读取,循环
            while((n=in.read(buff))!=-1){
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff,0,n);
            }
            //强制将缓存区的数据进行输出
            outputStream.flush();
            //关流
            outputStream.close();
            in.close();
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public void clearHistoryVideo() {
        CameraMonitorProperties cameraMonitor = ldProperties.getCameraMonitor();
        Integer historyStorageTotal = cameraMonitor.getHistoryStorageTotal();
        IPage<CameraMonitorInfoRespVo> iPage = new Page<>(1, historyStorageTotal);
        IPage<CameraMonitorInfoRespVo> iPage1 = cameraMonitorInfoMapper.selectPageList(iPage);
        int size = iPage1.getRecords().size();
        if (historyStorageTotal == size) {
            CameraMonitorInfoRespVo cameraMonitorInfoRespVo = iPage1.getRecords().get(size - 1);
            Long id = cameraMonitorInfoRespVo.getId();
            List<CameraMonitorInfoModel> cameraMonitorInfoRespVoList = cameraMonitorInfoMapper.selectListById(id);
            if (!CollectionUtils.isEmpty(cameraMonitorInfoRespVoList)) {
                for (CameraMonitorInfoModel data : cameraMonitorInfoRespVoList) {
                    cameraMonitorInfoMapper.deleteById(data.getId());
                    File file = new File(data.getFilePath());
                    if (file.exists()) {
                        file.delete();
                    }
                }
            }


        }
    }


}
